﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using VanTaiBLL;
using VanTaiProperty;

namespace VanTaiTMS.SubPage
{
    public partial class Uc_Khoi : UserControl
    {
        private readonly clsTMBlockBusiness _clsKhoi = new clsTMBlockBusiness();
        private DataTable _dtKhoi;
        protected void Page_Load(object sender, EventArgs e)
        {
            imgSave.ToolTip = "Thêm mới";
            imgSave.Visible = true;
        }
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {
        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTenKhoi.Text == "")
            {
                lbucthongbaoLoi.Text = "Vui lòng điền vào tên Khối !";
                OpenDialog("#ucthongbao");
                updatePanelucthongbao.Update();
            }
            if (txtMaKhoi.Text == "")
            {
                lbucthongbaoLoi.Text = "Vui lòng điền vào mã Khối !";
                OpenDialog("#ucthongbao");
                updatePanelucthongbao.Update();
            }
            if (txtMaKhoi.Text != "" && txtTenKhoi.Text != "")
            {
                InsertKhoi();
            }
        }
        protected void cmdClose_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaoxoa");
        }

        protected void cmdCloseucthongbao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbao");
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            _dtKhoi = _clsKhoi.Search("");
            if (_dtKhoi != null && _dtKhoi.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtKhoi.Rows)
                {
                    if (txtMaKhoi.Text == dr["MAKhoi"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertnName()
        {
            bool kiemtra = false;
            _dtKhoi = _clsKhoi.Search("");
            if (_dtKhoi != null && _dtKhoi.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtKhoi.Rows)
                {
                    if (txtTenKhoi.Text == dr["TENKhoi"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertKhoi()
        {
            try
            {
                if (CheckInsert())
                {
                    lbucthongbaoLoi.Text = "Mã Khối này đã tồn tại !";
                    OpenDialog("#ucthongbao");
                    updatePanelucthongbao.Update();
                }
                else
                {
                    if (CheckInsertnName())
                    {
                        lbucthongbaoLoi.Text = "Tên Khối này đã tồn tại !";
                        OpenDialog("#ucthongbao");
                        updatePanelucthongbao.Update();
                    }
                    else
                    {
                        var obj = new clsTMBlockProperty
                        {
                            Block_Alias = txtMaKhoi.Text,
                            Block_Name = txtTenKhoi.Text,
                            Block_Name_C = txtTenKhoiC.Text,
                            Block_Name_E = txtTenKhoiE.Text,
                            Block_Note = txtGhiChu.Text
                        };
                        clsTMBlockBusiness.InsertTMBlock(obj);
                        lbucthongbaoLoi.Text = "Thêm mới thành công mẫu tin !";
                        OpenDialog("#ucthongbao");
                        updatePanelucthongbao.Update();
                        ClearTextbox();
                        txtMaKhoi.Text = "MT000" + MaTuDong();
                    }
                }
            }
            catch (Exception ex)
            {
                lbucthongbaoLoi.Text = ex.ToString();
                updatePanelucthongbao.Update();
            }
        }
        #region[MaTangTuDong]

        protected int MaTuDong()
        {
            int i = 0;
            _dtKhoi = _clsKhoi.Search("");
            if (_dtKhoi != null && _dtKhoi.Rows.Count > 0)
            {
                int max = Convert.ToInt32(_dtKhoi.Rows[0]["STT"].ToString());
                for (i = 0; i < _dtKhoi.Rows.Count; i++)
                {
                    if (int.Parse(_dtKhoi.Rows[i]["STT"].ToString()) > max)
                    {
                        max = Convert.ToInt32(_dtKhoi.Rows[i]["STT"].ToString());
                    }
                }
                i = max + 1;
            }
            return i;
        }

        #endregion
        #region[ClearTextBox]

        protected void ClearTextbox()
        {
            txtGhiChu.Text = "";
            txtTenKhoiE.Text = "";
            txtTenKhoiC.Text = "";
            txtTenKhoi.Text = "";
            txtMaKhoi.Text = "";
        }

        #endregion

        #region[OpenDialog]

        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }

        private void CloseDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }

        #endregion
    }
}